package com.bjsxt.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;
@Controller
public class AliyunPYController {
    // 应用标识
    private final String APP_ID = "2016102600765521";
    // 应用私钥
    private final String APP_PRIVATE_KEY = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCdO+L1uzgrsoLrHtlx4e1gysZkttMY8pNVm47ryiCobtb2nmgfOmT2cTlA3l4t1mZ2saqKZQorXkA/hVUpyg0z0/Gt25Z7Cwe+IzmpLWOE/eLAs83Kwa6NxG8XCmqKRNB+nQv8UkcammRuXO1RYc1JMQg4f4RdOm4NpZRQ/XIh0414aFpVh/cDFlX2vXrqdHf/c7AByqAFXWXobcbAN9xacWErufb5qs77LwlM9pn13A/p3ePL0MggpSPVlEi0RFy4D0VY9xQ0f9Ek4cynRK6HvGJbJYi246bzjjRyXVRuW7+RvB66kTUZxYdhwGy7/gtKreuwZGkyUydhSfoqoVu/AgMBAAECggEAc6qBbIm8aZDZdENubLNp36Frzt0bb6fLMNfNjLFV0vIu2p9vM0HO0eg62Ph8SbYGDFzoewSXNY4RL2lTW/mSOsU4bdAy4/MaUAozubt1EXl/XIko43YVhZ6ypj14YJz8z5eSIJ0aR3y6UuGzhwGudaBHTuCnq7l3Cjz7XZUEZ9K9iLp9OuLT+CNqUkF5GP2hC7+Q6WkkZNQBUfDFBze12+6k9uJY3uTF0sLuwqPLmDIK74Vpc51IjBqnNCvfrqzOaOx6mw0pn3Xu3SIw9O4btaVF48/BG7/nc+TEiLcfTXfPAhFENqOq75ailBxUvUpLIXk+jWsaE1M268YiUQ6IAQKBgQD5Hc6YFGknO+FxEE8nPsh/OwbgRpUDOsUDi93woGzRoaoauDcmLP7YO1mAqcnDvBUbkCTb5PBJdL7A0lNMeIWLNuV8blizoUxOEpT7ScJCm4WhM5g4Qala4/Ov1UvksHZimVe8YAaEKQcq8V1E2msvjnWxXCUTbI6Et1nDV1svvwKBgQChlB+mhK8zofnp41Cp0UM8vmkvwIItozLEISHIHdMTU2yHKqVIuXDaw0rOtHuWiwLnUnOY+dszLAYPKd99Q2T9IgOaagBTu21ASZDwcap0nHU5RXhrfmSIHjcrzoLiZbT/SsUWmLY20IZcbKemcQXdfnvS6zZZEVlXJSrBn0TUAQKBgDPFkg3hEAlrVGre7NRHI/vtFHy/rnBBYnheNP2xGWxnoKZ06mEwsLXXf75RivD2wZw81KwuZBGgBZSlIVKfKYHJIuA9Pon2V5lSG0wuBAU7tQXPZy8p1jxha197gI5CbhgHrkgj3LbSn/64sp2Alw4HGUaG0925jdTfFfSWzwiZAoGAO1fxv/DfgDEvXo2UZKps7XRpoZYsuaJE1O/uPldl3KU5KPOqvOkO3+n2IbD76zCnIlqQBlFOBp9Wol8YrCMChk3l0OK5uAcQexoJ16WHm0iTbTNspN5JAg1G6hnwzEHaliDZ/hCca8lub6egc2rzYoQ/6hllXpUWoQEPkQlz+AECgYAZGVNIS7r6hGYqKoMGwoYaeX1Uj0rLc2X5c753lygBvybKPu75jLvj12hA3vqGU+yIF/Cdz+GwizcT/KHvMcc6DoEqhm6+nIRvbsAxFd6YgiJPoyJ8GmpY6Hn2Rvqe0RcrsiWM2posKYU0jtJETMcOLySE2+XUGi1PIdRd+TxIkw==";
    // 编码格式
    private final String CHARSET = "UTF-8";
    // 支付宝公钥
    private final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi4+VCInRylcikdajQl4kH32YOSPpb+s6gGPC6yVAQeqS90aMAK6UCX9ckml1nqhShjHJ848U2PtDdkydwwa2g1DazhJvaejyDRuHVrXh84LhezOiYzLPxG4JEwraEGbEPZQMoASkPm27AfElkFi9MM3ELXpOdy9UXeDSt5X0nYYOHezgDSMFF+Ce4laU/8vcWsCIN3Vn61nWpi5C4X0PfRgDOHuBX3HilpZHAvcQ9UaUwXF/NqX5z17fSUK555yHJB1GlfPmIn5VLPM5nCpNtlrWocKlkMCI7C6bnNOpoQO5N7imQBtyNz2jqrIdSw7xQszZaHvmCMpg5XDSi+mwLwIDAQAB";
    // 沙箱路径接口，正是路径应为https://openapi.alipay.com/gateway.do
    private final String GATEWAY_URL = "https://openapi.alipaydev.com/gateway.do";
    // 参数返回格式
    private final String FORMAT = "JSON";
    // 签名方式
    private final String SIGN_TYPE = "RSA2";
    // 支付宝异步通知路径，付款完毕后会异步调用本项目的方法，必须为公网地址
    private final String NOTIFY_URL = "http://127.0.0.1/notifyURL";
    // 支付宝同步通知路径，也就是当付款完毕后跳转本项目的页面，可以不是公网地址
    private final String RETURN_URL = "http://127.0.0.1/returnURL";


    @RequestMapping("alipay")
    public void alipay(HttpServletResponse httpResponse) throws IOException {
        // 实例化客户端,填入所需参数
        // 网关地址 应用标识 应用私钥 传输格式 编码格式 支付宝公钥 签名类型
        AlipayClient alipayClient = new DefaultAlipayClient(GATEWAY_URL, APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        //在公共参数中设置回跳和通知地址
        request.setReturnUrl(RETURN_URL);
        request.setNotifyUrl(NOTIFY_URL);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        //生成随机Id
        String out_trade_no = UUID.randomUUID().toString();
        //付款金额，必填
        String total_amount =Integer.toString(100);
        //订单名称，必填
        String subject ="奥迪A8 2016款 A8L 60 TFSl quattro豪华型";
        //商品描述，可空
        String body = "尊敬的会员欢迎购买奥迪A8 2016款 A8L 60 TFSl quattro豪华型";
        request.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        String form = "";
        try {
            form = alipayClient.pageExecute(request).getBody(); // 调用SDK生成表单
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType("text/html;charset=" + CHARSET);
        httpResponse.getWriter().write(form);// 直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }

}
